﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Reflection;
using System.Xml;
using System.IO;

namespace WoChu.RfClient.Common
{
    /// <summary>
    /// Summary description for Settings.
    /// </summary>
    public class AppSetting
    {

        private static AppSetting _NewInstance = new AppSetting();

        public static AppSetting NewInstance
        {
            get
            {
                if (_NewInstance == null)
                {
                    _NewInstance = new AppSetting();
                }

                return _NewInstance;
            }
        }

        Hashtable m_list = new Hashtable();

        string m_filePath = string.Empty;
        public string FilePath
        {
            get { return m_filePath; }
            set { m_filePath = value; }
        }

        public object this[string key]
        {
            get { return m_list[key]; }
            set { m_list[key] = value; }
        }

        public string GetString(string key)
        {
            object result = m_list[key];
            return (result == null) ? String.Empty : result.ToString();
        }

        public void SetString(string key, string value)
        {
            m_list[key] = value;
        }

        public int GetInt(string key)
        {
            object result = m_list[key];
            return (result == null) ? 0 : Convert.ToInt32(result);
        }

        public ICollection iKeys
        {
            get
            {
                return m_list.Keys;
            }
        }

        public int iCount
        {
            get
            {
                return m_list.Count;
            }
        }

        public bool GetBool(string key)
        {
            object result = m_list[key];
            return (result == null) ? false : Convert.ToBoolean(result);
        }

        public AppSetting()
        {
            m_filePath = GetFilePath();
            Read();
        }

        private string GetFilePath()
        {
            return Assembly.Load("WoChu.RfClient.Entry").GetName().CodeBase + ".config";
        }

        public void Read()
        {
            try
            {
                m_list.Clear();

                XmlTextReader reader = new XmlTextReader(m_filePath);

                while (reader.Read())
                {
                    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "add"))
                    {
                        m_list[reader.GetAttribute("key")] = reader.GetAttribute("value");
                    }
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
        }

        public void Write()
        {
            try
            {
                // header elements
                StreamWriter writer = File.CreateText(m_filePath);
                writer.WriteLine("<configuration>");
                writer.WriteLine("\t<appSettings>");

                // go through internal list and create 'add' element for each item
                IDictionaryEnumerator enumerator = m_list.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    writer.WriteLine("\t\t<add key=\"{0}\" value=\"{1}\" />",
                        enumerator.Key.ToString(),
                        enumerator.Value);
                }

                // footer elements
                writer.WriteLine("\t</appSettings>");
                writer.WriteLine("</configuration>");

                writer.Close();
            }
            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
        }
        //录写日志
        //public void Writestr(string readme)
        //{
        //    //debug==================================================
        //    //StreamWriter dout = new StreamWriter(@"c:\" + System.DateTime.Now.ToString("yyyMMddHHmmss") + ".txt");
        //    //StreamWriter dout = new StreamWriter(@"D:\我厨代码管理\WuWei\PDASyncService\WoChu.PDA.SyncService\bak\" + "Log" + DateTime.Now.ToString("yyyyMMdd") + ".txt", true);
        //    StreamWriter dout = new StreamWriter(@"\Program Files\wochu.rfclient.entry\bak\" + "Log" + DateTime.Now.ToString("yyyyMMdd") + ".txt", true);
        //    dout.Write("\r\n操作时间：" + System.DateTime.Now.ToString() + "\r\n事件：" + readme);
        //    //debug==================================================
        //    dout.Close();
        //}
    }
}
